package com.example.controller;

import com.example.entity.Product;
import com.example.service.ProductService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//开启热部署
@RefreshScope
@RestController
@RequestMapping("/product")
public class ProductController {

    @Value("${product.productId}")
    private String productId;
    @Value("${product.productName}")
    private String productName;
    @Autowired
    private ProductService productService;
    @GetMapping("/getNacos")
    public String getNacos() {
        return productId + ":" + productName;
    }
    /**
     * @PathVariable("id")：表明是路径变量，从路径中取出id的值就是方法输入的参数id
     * @param id
     */
    @GetMapping("/{id}")
    public Product getProduct(@PathVariable("id") Long id,
                              HttpServletRequest request) {
        System.out.println("X-Token：" + request.getHeader("X-Token"));
        System.out.println("hello");
        Product product = productService.getProductById(id);
        return product;
    }
}
